
#include<iostream>
#include<sw/redis++/redis++.h>
#include<vector>
#include<string>
#include<thread>
#include<chrono>
#include<set>
#include<unordered_map>
#include"Util.hpp"
using namespace std;
using namespace chrono_literals;

void test1(sw::redis::Redis& redis)
{
       cout << "sadd和smember" << endl;
       redis.flushall();

       //一次1个元素
       redis.sadd("key","111");
       //初始化列表一次多个元素
       redis.sadd("key",{"222","333","444"});
       //容器
       set<string> s =  {"555","666"};
       redis.sadd("key",s.begin(),s.end());

       set<string> result;
       redis.smembers("key",std::inserter(result,result.end()));
       printContainer(result);

}

void test2(sw::redis::Redis& redis)
{
      cout << "sismember" << endl;
      redis.flushall();

      redis.sadd("key",{"111","222","333"});
      bool ret = redis.sismember("key","212");
      if(ret) cout << "member存在" << endl;
      else cout << "member不存在" << endl;
}

void test3(sw::redis::Redis& redis)
{
      cout << "sismember" << endl;
      redis.flushall();

      redis.sadd("key",{"111","222","333"});
      cout << "len: " << redis.scard("key")<< endl;
      
}

void test4(sw::redis::Redis& redis)
{
      cout << "spop" << endl;
      redis.flushall();

      redis.sadd("key",{"111","222","333"});
      auto ret = redis.spop("key");
      if(ret) cout << ret.value() << endl;
      else cout << "ret无效" << endl;
}

void test5(sw::redis::Redis& redis)
{
      cout << "sinter" << endl;
      redis.flushall();

      redis.sadd("key1",{"111","444","333"});
      redis.sadd("key2",{"333","111","555"});

      set<string> result;
      redis.sinter({"key1","key2"},std::inserter(result,result.end()));
      printContainer(result);
}

void test6(sw::redis::Redis& redis)
{
      cout << "sinterstore" << endl;
      redis.flushall();

      redis.sadd("key1",{"111","444","333"});
      redis.sadd("key2",{"333","111","555"});

      long long len = redis.sinterstore("key3",{"key1","key2"});
      cout << "inter len: " << len << endl;

      set<string> result;
       redis.smembers("key3",std::inserter(result,result.end()));
       printContainer(result);
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    test6(redis);
    return 0;
}